<template>
  <div class="template">
    <div class="template-title">What do you want to ideate today?</div>
    <div class="btn-tag-list">
      <a-button
        @click="onActiveTag(item)"
        v-for="(item,index) in tagList"
        :key="index"
        :type="activeTag === item ? 'primary' : ''"
        shape="round" >{{ item }}</a-button>
    </div>
    <div class="card-list">
      <div class="card-item" v-for="item in showCardList" :key="item.id" @click="getDetail(item)">
        <div class="card-title">{{ item.name }}</div>
        <div class="card-btn-tag-list" v-if="item.category">
          <a-button
            shape="round" class="nohover">{{ item.category }}</a-button>
        </div>
        <div class="card-content">
          <p style="margin-top: 20px">{{ item.description }}</p>
        </div>
      </div>
    </div>

    <a-modal
      :title="itemDetail.name"
      :visible="modalShow"
      :width="640"
      @cancel="modalShow = false"
    >
      <template #footer>
        <a-button key="back" @click="modalShow = false">CANCEL</a-button>
        <a-button key="submit" type="primary" @click="handleConfirm">USE</a-button>
      </template>
      <div class="question-content">
        <p class="question-item" v-for="(qItem,qIndex) in questions" :key="qItem.id">{{ qIndex + 1 }}: {{ qItem.content }}</p>
      </div>
    </a-modal>
  </div>
</template>

<script>
import { ideaTemplates } from '@/api/api'

export default {
  name: 'Index',
  data () {
    return {
      activeTag: 'All',
      tagList: [],
      cardList: [
      ],
      showCardList: [],
      itemDetail: {
      },
      questions: [
      ],
      modalShow: false
    }
  },
  created () {
    ideaTemplates.list().then(res => {
      const data = res.idea_templates
      this.cardList = data
      // 过滤出所有条件
      const tagList = data.map(el => el.category).filter(el => el)
      this.tagList = Array.from(new Set(tagList))
      this.tagList = ['All', ...this.tagList]
      this.onActiveTag(this.tagList[0])
    })
  },
  methods: {
    onActiveTag (item) {
      this.activeTag = item
      console.log(item)
      if (item === 'All') {
        this.showCardList = [...this.cardList]
        return
      }
      this.showCardList = this.cardList.filter(el => !el.category || el.category === this.activeTag)
    },
    getDetail (item) {
      this.itemDetail = item
      ideaTemplates.read(item.id).then(res => {
        this.questions = res.questions
        this.modalShow = true
      })
    },
    handleConfirm () {
      console.log('sadadd')
      this.$router.push({
        path: '/Dashboard/create',
        query: {
            name: this.itemDetail.name,
           templateId: this.itemDetail.id
        }
      })
      this.modalShow = false
    }
  }
}
</script>

<style scoped lang='less'>
.template {
  ::v-deep .ant-btn {
    font-family: Quicksand700, serif !important;
    margin-left: 25px;
    font-weight: bold;
    font-size: 16px;
    height: 28px;
  }
  ::v-deep .card-btn-tag-list .ant-btn {
    margin-left: 0;
    margin-right: 10px;
  }
}
.template-title {
  height: 53px;
  font-size: 18px;
  font-family: Quicksand700;
  font-weight: bold;
  color: #000000;
  line-height: 53px;
}
.btn-tag-list {
  display: flex;
  align-items: center;
}
.card-list {
  display: flex;
    flex-wrap: wrap;
  .card-item {
    cursor: pointer;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    width: 332px;
    height: 320px;
    background: #FFFFFF;
    border-radius: 4px 4px 4px 4px;
    padding: 24px;
    padding-top: 0;
    margin-top: 16px;
    margin-right:16px;
      display: flex;
      flex-direction: column;
    .nohover:hover,
    .nohover:focus{
      color: inherit;
      border-color: #d9d9d9;
      cursor: default;
    }
  }
    .card-content {
        flex-grow: 1;
        overflow: hidden;
    }
  .card-title {
    font-size: 24px;
    font-family: Quicksand500;
    font-weight: 500;
    color: #069697;
    line-height: 30px;
    padding-top: 20px;
    padding-bottom: 10px;
  }
  p {
    margin-bottom: 0;
    padding-bottom: 0;
    font-size: 16px;
    line-height: 24px;
  }
}
.question-content {
  padding: 0 20px;
}
</style>
